<?php defined("MAIN_DIR") or die("No direct access");

$atm_id = UV::readInt($route->getPath(4));

$edit_error = false;
if (isset($_POST['action']) && $_POST['action'] == "edit")
{
	$address = trim(filter_var($_POST['address'],FILTER_SANITIZE_MAGIC_QUOTES));
	$place = trim(filter_var($_POST['place'],FILTER_SANITIZE_MAGIC_QUOTES));
	$currency = trim(filter_var($_POST['currency'],FILTER_SANITIZE_MAGIC_QUOTES));
	$work_time = trim(filter_var($_POST['work_time'],FILTER_SANITIZE_MAGIC_QUOTES));
	$edit_query = "update `atm` set `bank_id`='$_POST[bank]',`place`='$place',`address`='$address',`currency`='$currency',`work_time`='$work_time' where `atm_id` = '$atm_id' limit 1";
	if (!mysql_query($edit_query))
		$edit_error = true;
}
if ($atm_id) {
	$atm_res = mysql_query("select * from `atm` where `atm_id` = '$atm_id' limit 1");
	if (mysql_num_rows($atm_res) == 0) {
		to("/admin/atms/");
	}
	$atm_row = mysql_fetch_array($atm_res);
}

require_once ADMIN.HEADFILE;
?>
<div align="left">
<h1>Редактирование банкомата</h1>
<hr /><?php
if (isset($_POST['action']) && $_POST['action'] == "edit" && !$edit_error)
	echo "<p style='color:green'>Успешно отредактировано</p>";
elseif ($edit_error)
	echo "<p style='color:red'>Не удалось отредактировать</p>";
?><form method='POST'>
<input type='hidden' name='action' value='edit' />
<input type='hidden' name='atm_id' value='<?php echo $atm_row['atm_id']; ?>' />
<table cellpadding='5' cellspacing='0'>
	<tr>
		<td>ID</td>
		<td><input style="width:200px" type='text' name='atm_id' value='<?php echo $atm_row['atm_id'];?>' disabled /></td>
	</tr>
	<tr>
		<td>Банк</td>
		<td>
			<select style="width:200px" name='bank'><?php
			$bank_res = mysql_query("select `bank_id`,`name` from `bank` where `city_id` = '$_SESSION[city_admin]' order by `name`");
			while ($bank_row = mysql_fetch_array($bank_res))
			{
				echo "<option value='$bank_row[bank_id]'";
				if ($bank_row['bank_id'] == $atm_row['bank_id'])
					echo " selected='selected'";
				echo ">$bank_row[name]</option>";
			}
			?></select>
		</td>
	</tr>
	<tr>
		<td>Адрес</td>
		<td><input style='width:100%' type='text' name='address' value='<?php echo $atm_row['address'];?>' /></td>
	</tr>
	<tr>
		<td>Объект</td>
		<td><input style="width:100%" type='text' name='place' value='<?php echo $atm_row['place']?>' /></td>
	</tr>
	<tr>
		<td>Валюта</td>
		<td><input style='width:100%' type='text' name='currency' value='<?php echo $atm_row['currency']?>' /></td>
	</tr>
	<tr>
		<td>Время работы</td>
		<td><input style='width:100%' type='text' name='work_time' value='<?php echo $atm_row['work_time']?>' /></td>
	</tr>
	<tr valign='top'>
		<td colspan='2' align="right"><button style="width:100px;height:30px" type="submit">Сохранить</button></td>
	</tr>
</table>
</form>
<br /><hr /><br />
</div>

<?php require_once ADMIN.FOOTERFILE; ?>